package com.googlecode.monitoring.webpages.dao;

import com.googlecode.monitoring.dao.XmlUtils;
import com.googlecode.monitoring.exception.DataDaoException;
import com.googlecode.monitoring.webpages.model.WebPage;
import com.googlecode.monitoring.webpages.model.WebPageItemToCheck;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.List;

/**
 * Author: Sergiy Doroshenko
 * Date: Aug 30, 2010
 * Time: 3:19:00 PM
 */
public class WebPagesAgentXmlUtils extends XmlUtils {

    public WebPageItemToCheck buildDataToCheck(Element xmlItemElmnt) {
        String name = xmlItemElmnt.getAttribute("name");
        if (name.isEmpty())
            throw new DataDaoException("item with empty name");
        String id = xmlItemElmnt.getAttribute("id");
        if (id.isEmpty())
            throw new DataDaoException("item with empty id");
        WebPageItemToCheck dbItem = new WebPageItemToCheck();
        dbItem.setName(name);
        dbItem.setId(id);

        Node webPageNode = xmlItemElmnt.getElementsByTagName("webPage").item(0);
        Node timeEventNode = xmlItemElmnt.getElementsByTagName("eventTime").item(0);
        NodeList personNode = xmlItemElmnt.getElementsByTagName("person");
        dbItem.setWebPage(buildWebPageFromNode(webPageNode));
        dbItem.setEventTime(buildEventTime(timeEventNode));
        dbItem.setResponsiblePersons(buildPersons(personNode));
        return dbItem;
    }

    private WebPage buildWebPageFromNode(Node node) {
        WebPage webPage = new WebPage();
        if (node.getAttributes().getNamedItem("url") != null) {
            webPage.setUrl(node.getAttributes().getNamedItem("url").getNodeValue());
        }
        if (node.getAttributes().getNamedItem("textToFind") != null) {
            webPage.setTextToFind(node.getAttributes().getNamedItem("textToFind").getNodeValue());
        }
        return webPage;
    }

    public Element buildWebPageItemToCheckElement(Document doc, WebPageItemToCheck item, SchedulerFactoryBean schedulerFactoryBean) throws Exception {
        Element itemEl = doc.createElement("item");
        Element webPageEl = doc.createElement("webPage");
        Element stateEl = doc.createElement("state");
        Element timeEventEl = doc.createElement("eventTime");
        Element lastCheckTime = doc.createElement("lastCheckTime");
        Element nextCheckTime = doc.createElement("nextCheckTime");

        lastCheckTime.setAttribute("value", item.getLastCheckTime() == null ? "" : item.getLastCheckTime().toString(dateTimeFormat));
        nextCheckTime.setAttribute("value", getNextCheckTime(item, schedulerFactoryBean).toString(dateTimeFormat));

        itemEl.setAttribute("id", item.getId());
        itemEl.setAttribute("name", item.getName());

        webPageEl.setAttribute("url", item.getWebPage().getUrl());
        webPageEl.setAttribute("textToFind", item.getWebPage().getTextToFind());

        timeEventEl.setAttribute("cron", item.getEventTime().getCron());

        List<Element> personElList = buildPersonNodeList(doc, item);
        if (item.getCurrentStateName() != null) {
            stateEl.setAttribute("name", item.getCurrentStateName().getName());
        }


        itemEl.appendChild(webPageEl);
        itemEl.appendChild(timeEventEl);
        for (Element personEl : personElList) {
            itemEl.appendChild(personEl);
        }
        itemEl.appendChild(stateEl);
        itemEl.appendChild(lastCheckTime);
        itemEl.appendChild(nextCheckTime);

        return itemEl;
    }


    
}
